package model;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;

public class Reponse {

	
	private String intitule;
	private Boolean solution;
	
	public Reponse() {
		
	}
	
	public Reponse(String intitule, Boolean solution) {
		this.intitule = intitule;
		this.solution = solution;
	}

	
	public String getIntitule()
	{
		return intitule;
	}
	
	public void setIntitule(String intitule_)
	{
		this.intitule = intitule_;
	}

	public Boolean isSolution() {
		return solution;
	}

	public void setSolution(Boolean solution) {
		this.solution = solution;
	}
	
	public void addReponseBD(int _idQ, String _intitule, Boolean _solution) throws ClassNotFoundException{

		String url = "jdbc:mysql://localhost:3306/Base";
		String utilisateur = "root";
		String motdepasse = "mysqlroot";
		Connection connexion = null;
		
		try{
			Class.forName("com.mysql.jdbc.Driver");
			connexion = (Connection) DriverManager.getConnection(url, utilisateur, motdepasse);
			
	    	PreparedStatement statement = connexion.prepareStatement("INSERT into reponses (idQ, intitule, solution) VALUES (?,?,? )");
	    	 
	    	statement.setInt(1, _idQ);
	    	statement.setString(2, _intitule);
	    	statement.setBoolean(3, _solution);
	    	
	    	 
	    	statement.executeUpdate();
	    	System.out.println("réponse ajoutée avec succès");
	    	
			}catch ( SQLException e ) {
	        	System.out.println("Erreur lors de l'ajout de la réponse");
				} finally {
		            if ( connexion != null )
		                try {
		                    connexion.close();
		                	} catch ( SQLException ignore ) {
		          }
		}
	}
	

}
